package com.jy.controller;

import com.jy.common.CommonConstant;
import com.jy.pojo.FileDetails;
import com.jy.service.FileService;
import com.jy.service.FileTypeService;
import com.jy.service.FolderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.apache.commons.codec.binary.Base64;

import java.nio.charset.StandardCharsets;
import java.util.Map;

/**
 * 页面跳转控制类
 */
@Controller
public class IndexController {

    @Autowired
    private FileService fileService;
    @Autowired
    private FolderService folderService;
    @Autowired
    private FileTypeService fileTypeService;
    /**
     * 跳转首页
     * @return
     */
    @RequestMapping({"/","/index"})
    public String toIndex(Model model){
        return "indexTwo";
    }   

    /**
     * 跳转到文件夹页面
     * @return
     */
    @RequestMapping("/folder.htm/{folderId}")
    public String toFileFolder(Model model,@PathVariable("folderId") Integer fileId){
        //获取当前文件的目录信息，及地址id信息
        Map<String, Object> dirs = fileService.getDirs(fileId);
        //根据文件编号获取文件内容
        FileDetails fileByFileId = fileService.getFileByFileId(fileId);
        //将文件内容传入前台页面上
        model.addAttribute("fileDetail",fileByFileId);
        model.addAttribute("fileId",fileId);
        if(fileByFileId.getIsDir()==0){
            String dirStr=dirs.get("dirs").toString();
            String dirIdsStr=dirs.get("dirIds").toString();
            model.addAttribute("fileDirs",dirStr.substring(0,dirStr.lastIndexOf(CommonConstant.DIR_SPLIT)));
            model.addAttribute("fileDirIds",dirIdsStr.substring(0,dirIdsStr.lastIndexOf(CommonConstant.DIR_SPLIT)));
        }else{
            model.addAttribute("fileDirs",dirs.get("dirs"));
            model.addAttribute("fileDirIds",dirs.get("dirIds"));
        }
        return "floder/floderDetails";
    }

    /**
     * 跳转到文件详情页面
     * @return
     */
    @RequestMapping("/details.htm/{fileId}")
    public String toFileDetails(Model model,@PathVariable("fileId") Integer fileId){
        //获取当前文件的目录信息，及地址id信息
        Map<String, Object> dirs = fileService.getDirs(fileId);
        //根据文件编号获取文件内容
        FileDetails fileByFileId = fileService.getFileByFileId(fileId);
        //将文件内容传入前台页面上
        model.addAttribute("fileDetail",fileByFileId);
        model.addAttribute("fileId",fileId);
        if(fileByFileId.getIsDir()==0){
            String dirStr=dirs.get("dirs").toString();
            String dirIdsStr=dirs.get("dirIds").toString();
            model.addAttribute("fileDirs",dirStr.substring(0,dirStr.lastIndexOf(CommonConstant.DIR_SPLIT)));
            model.addAttribute("fileDirIds",dirIdsStr.substring(0,dirIdsStr.lastIndexOf(CommonConstant.DIR_SPLIT)));
        }else{
            model.addAttribute("fileDirs",dirs.get("dirs"));
            model.addAttribute("fileDirIds",dirs.get("dirIds"));
        }
        //跳转到文件详情页面
        return "file/fileDetails";
    }

    /**
     * 跳转到文件查询页面
     * @return
     */
    @RequestMapping("/search/{searchText}")
    public String toFileSearch(Model model,@PathVariable("searchText") String searchText){
        //文件名解码
        String text=new String(Base64.decodeBase64(searchText), StandardCharsets.UTF_8);
//        List<FileType> allFileType = fileTypeService.getAllFileType();
//        model.addAttribute("fileTypes",allFileType);
        model.addAttribute("searchText",text);
        return "fileSearchTwo";
    }
}
